package com.ibeeking.gmc.global.g.rest.controller.v1;

import com.ibeeking.gmc.global.g.rest.service.IAreaService;
import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.gmc.global.service.common.param.AreaParam;
import com.ibeeking.gmc.global.service.common.query.AreaPageQuery;
import com.ibeeking.gmc.global.service.common.vo.AreaPageVO;
import com.ibeeking.gmc.global.service.common.vo.AreaTreeVO;
import com.ibeeking.gmc.global.service.common.vo.AreaVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.base.BaseValidationGroup;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @ClassName AreaController控制器
 * @Description 地区控制器
 * @Author ibeeking
 * @Date 2020-12-23 13:43
 **/
@RestController
@RequestMapping("/g/area/v1")
public class AreaController {

    @Resource
    private IAreaService areaServiceImpl;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "有效地区下拉列表查询")
    @GetMapping(value = "/listByParentCode/{parentCode}")
    public List<AreaPageVO> listByParentCode(@PathVariable @NotBlank(message = "地区编码不能为空") String parentCode) {
        return areaServiceImpl.listByParentCode(parentCode);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "有效地区下拉列表查询")
    @GetMapping(value = "/treeListByParentCode/{parentCode}")
    public List<AreaTreeVO> treeListByParentCode(@PathVariable @NotBlank(message = "地区编码不能为空") String parentCode) {
        return areaServiceImpl.treeListByParentCode(parentCode);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "分页地区列表")
    @PostMapping(value = "/pageList")
    public Page<AreaPageVO> pageList(@RequestBody AreaPageQuery areaPageQuery) {
        return areaServiceImpl.pageList(areaPageQuery);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "地区树形列表")
    @GetMapping(value = "/treeList/{parentCode}")
    public List<AreaPageVO> treeList(@PathVariable @NotBlank(message = "上级编码不能为空") String parentCode) {
        return areaServiceImpl.treeList(parentCode);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "地区新增")
    @PostMapping(value = "/add")
    public Boolean add(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Insert.class) AreaParam areaParam) {
        areaParam.setCreateBy(currentUser.getId());
        return areaServiceImpl.add(areaParam);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "地区修改")
    @PostMapping(value = "/modify")
    public Boolean modify(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Update.class) AreaParam areaParam) {
        areaParam.setModifyBy(currentUser.getId());
        return areaServiceImpl.modify(areaParam);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "地区查询")
    @GetMapping(value = "/view/{id}")
    public AreaVO view(@PathVariable("id") Long id) {
        return areaServiceImpl.view(id);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "code查询地区")
    @PostMapping(value = "/areaInfoByCode/{code}")
    public AreaVO areaInfoByCode(@PathVariable("code") String code) {
        return areaServiceImpl.areaInfoByCode(code);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "地区修改状态")
    @GetMapping(value = "/changeStatus/{id}/{status}")
    public Boolean changeStatus(@LoginUser CurrentUser currentUser, @PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return areaServiceImpl.changeStatus(id, status, currentUser.getId());
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "地区删除")
    @GetMapping(value = "/del/{id}")
    public Boolean del(@PathVariable("id") Long id) {
        return areaServiceImpl.del(id);
    }
}
